<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="150px">
      <el-form-item label="用户名" prop="username">
        <el-input v-model="dataForm.username" placeholder="用户名" :disabled="true"></el-input>
      </el-form-item>
      <el-form-item label="站点个数 " prop="webOpenSite">
        <el-select v-model="dataForm.siteSize" placeholder="请选择">
          <el-option
            v-for="item in webOpenSites"
            :key="item.webOpenSite"
            :label="item.webOpenSite"
            :value="item.webOpenSite">
          </el-option>
        </el-select>
      </el-form-item>
        <el-form-item  label="用户类型" prop="userType">
          <el-select v-model="dataForm.userType" placeholder="请选择用户类型" style="width: 100%">
            <el-option
              v-for="item in types"
              :key="item.type"
              :label="item.label"
              :value="item.type">
            </el-option>
          </el-select>
        </el-form-item>
      <el-form-item label="广告位个数" prop="webOpenAdv">
        <el-select v-model="dataForm.advSize" placeholder="请选择">
          <el-option
            v-for="item in webOpenAdvs"
            :key="item.webOpenAdv"
            :label="item.webOpenAdv"
            :value="item.webOpenAdv">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="视频开闭" prop="webOpenType">
        <el-select v-model="dataForm.videoType" placeholder="请选择">
          <el-option
            v-for="item in videoTypes"
            :key="item.videoType"
            :label="item.label"
            :value="item.videoType">
          </el-option>
        </el-select>
      </el-form-item>

      <div v-if="dataForm.type !== 1">
      <el-form-item label="代理广告位级别" prop="webOpenType">
        <el-select v-model="dataForm.advLevel" placeholder="请选择">
          <el-option
            v-for="item in advLevels"
            :key="item.advLevel"
            :label="item.label"
            :value="item.advLevel">
          </el-option>
        </el-select>
      </el-form-item>
      </div>

      <el-form-item label="账户状态" prop="webOpenType">
        <el-select v-model="dataForm.status" placeholder="请选择">
          <el-option
            v-for="item in webOpenTypes"
            :key="item.webOpenType"
            :label="item.label"
            :value="item.webOpenType">
          </el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <basesite v-if="basesitevisible" ref="basesite"></basesite>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="sitemanage()">站点管理</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import API from '@/api'
  import Basesite from '../basesite/index'
  export default {
    data () {
      return {
        visible: false,
        basesitevisible: false,
        webOpenTypes: [{
          webOpenType: 0,
          label: '封禁中'
        }, {
          webOpenType: 1,
          label: '正常'
        }],
        types: [{
          type: 3,
          label: '普通用户'
        }, {
          type: 4,
          label: '分享用户'
        }],
        advLevels: [{
          advLevel: 1,
          label: '顶部广告位'
        }, {
          advLevel: 2,
          label: '顶部广告位+一个悬浮框'
        }, {
          advLevel: 3,
          label: '顶部广告位+两个悬浮框'
        }, {
          advLevel: 4,
          label: '顶部广告位+三个悬浮框'
        }],
        videoTypes: [{
          videoType: 0,
          label: '关闭'
        }, {
          videoType: 1,
          label: '开启'
        }],

        webOpenSites: [{
          webOpenSite: 5
        }, {
          webOpenSite: 10
        }, {
          webOpenSite: 15
        }, {
          webOpenSite: 20
        }, {
          webOpenSite: 25
        }, {
          webOpenSite: 30
        }],
        webOpenAdvs: [{
          webOpenAdv: 10
        }, {
          webOpenAdv: 20
        }, {
          webOpenAdv: 30
        }, {
          webOpenAdv: 40
        }, {
          webOpenAdv: 50
        }, {
          webOpenAdv: 60
        }],
        dataForm: {
          id: 0,
          status: '',
          userId: '',
          siteSize: '',
          advSize: '',
          type: '',
          userType: '',
          videoType: '',
          advLevel: ''
        },
        dataRule: {
          status: [
            { required: true, message: '状态不能为空', trigger: 'blur' }
          ],
          videoType: [
            { required: true, message: '视频开关不能为空', trigger: 'blur' }
          ],
          siteSize: [
            { required: true, message: '站点个数不能为空', trigger: 'blur' }
          ],
          advSize: [
            { required: true, message: '广告个数不能为空', trigger: 'blur' }
          ],
          type: [
            { required: true, message: '会员类型不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    components: {
      Basesite
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.basesitevisible = false
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            API.basemember.info(this.dataForm.id).then(({data}) => {
              console.log(data)
              if (data && data.code === 0) {
                console.log(data)
                this.dataForm.username = data.basemember.userName
                this.dataForm.userId = data.basemember.userId
                this.dataForm.status = data.basemember.status
                this.dataForm.siteSize = data.basemember.siteSize
                this.dataForm.advSize = data.basemember.advSize
                this.dataForm.type = data.basemember.type
                this.dataForm.userType = data.basemember.userType
                this.dataForm.videoType = data.basemember.videoType
                this.dataForm.advLevel = data.basemember.advLevel
              }
            })
          }
        })
      },
      // 新增 / 修改
      sitemanage () {
        console.log(this.dataForm.userId)
        this.basesitevisible = true
        this.$nextTick(() => {
          this.$refs.basesite.getDataListinit(this.dataForm.userId)
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            var params = {
              'id': this.dataForm.id || undefined,
              'status': this.dataForm.status,
              'createTime': this.dataForm.createTime,
              'siteSize': this.dataForm.siteSize,
              'advSize': this.dataForm.advSize,
              'type': this.dataForm.type,
              'userType': this.dataForm.userType,
              'videoType': this.dataForm.videoType,
              'advLevel': this.dataForm.advLevel
            }
            var tick = !this.dataForm.id ? API.basemember.add(params) : API.basemember.update(params)
            tick.then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
